package cn.terminus.service.customer.contractsellbystagesinfo;

import cn.terminus.biz.customer.contractsellbystagesinfo.ContractSellByStagesInfoManager;
import cn.terminus.client.customer.contractsellbystagesinfo.param.*;
import cn.terminus.client.customer.contractsellbystagesinfo.result.ContractSellByStagesInfoResult;
import cn.terminus.client.customer.contractsellbystagesinfo.service.ContractSellByStagesInfoService;
import cn.terminus.common.customer.bean.contractsellbystagesinfo.dto.*;
import cn.terminus.result.common.annotation.TerminusParamVilidator;
import cn.terminus.result.common.context.TerminusCallContext;
import cn.terminus.result.common.exception.DataAccessException;
import cn.terminus.result.common.exception.ServiceException;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import cn.terminus.result.common.result.TerminusResult;
import cn.terminus.result.common.util.BeanCopyUtils;
import cn.terminus.service.customer.common.AbstractTerminusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (ContractSellByStagesInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-07-21 11:17:20
 */
@Service
public class ContractSellByStagesInfoServiceImpl extends AbstractTerminusService implements ContractSellByStagesInfoService {
    /**
     * 日志
     */
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private ContractSellByStagesInfoManager contractSellByStagesInfoManager;

    /**
     * 新增
     *
     * @param param
     * @param context
     * @return
     * @throws ServiceException
     */
    @Override
    @TerminusParamVilidator(argsIndexs = {0, 1})
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public TerminusResult<String> add(ContractSellByStagesInfoNewParam param, TerminusCallContext context) throws ServiceException {
        ContractSellByStagesInfoNewDto newDto = new ContractSellByStagesInfoNewDto();
        newDto.setId(genUUID());
        BeanCopyUtils.copyProperties(param, context, newDto);
        try {
            contractSellByStagesInfoManager.add(newDto);
        } catch (DataAccessException e) {
            logger.error("新增失败,错误信息：{}", e.getMessage());
            throw new ServiceException(e);
        }
        return TerminusResult.success(String.valueOf(newDto.getId()));
    }

    /**
     * 更新
     *
     * @param param
     * @param context
     * @return
     * @throws ServiceException
     */
    @Override
    @TerminusParamVilidator(argsIndexs = {0, 1})
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public TerminusResult<Boolean> update(ContractSellByStagesInfoEditParam param, TerminusCallContext context) throws ServiceException {
        ContractSellByStagesInfoEditDto editDto = new ContractSellByStagesInfoEditDto();
        BeanCopyUtils.copyProperties(param, context, editDto);
        try {
            contractSellByStagesInfoManager.update(editDto);
        } catch (DataAccessException e) {
            logger.error("更新失败,错误信息：{}", e.getMessage());
            throw new ServiceException(e);
        }
        return TerminusResult.success(true);
    }

    /**
     * 删除
     *
     * @param param
     * @param context
     * @return
     * @throws ServiceException
     */
    @Override
    @TerminusParamVilidator(argsIndexs = {0, 1})
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public TerminusResult<Boolean> delete(ContractSellByStagesInfoDeleteParam param, TerminusCallContext context) throws ServiceException {
        ContractSellByStagesInfoDeleteDto deleteDto = new ContractSellByStagesInfoDeleteDto();
        BeanCopyUtils.copyProperties(param, context, deleteDto);
        try {
            contractSellByStagesInfoManager.delete(deleteDto);
        } catch (DataAccessException e) {
            logger.error("删除失败,错误信息：{}", e.getMessage());
            throw new ServiceException(e);
        }
        return TerminusResult.success(true);
    }


    /**
     * 查询
     *
     * @param param
     * @param context
     * @return
     * @throws ServiceException
     */
    @Override
    @TerminusParamVilidator(argsIndexs = {0, 1})
    public TerminusResult<List<ContractSellByStagesInfoResult>> query(ContractSellByStagesInfoQueryParam param, TerminusCallContext context) throws ServiceException {
        ContractSellByStagesInfoQueryDto queryDto = new ContractSellByStagesInfoQueryDto();
        BeanCopyUtils.copyProperties(param, queryDto);
        try {
            List<ContractSellByStagesInfoResultDto> queryResultList = contractSellByStagesInfoManager.query(queryDto);
            return TerminusResult.success(BeanCopyUtils.copy(queryResultList, ContractSellByStagesInfoResult.class));
        } catch (DataAccessException e) {
            logger.error("查询失败,错误信息：{}", e.getMessage());
            throw new ServiceException(e);
        }

    }

    /**
     * 查询
     *
     * @param param
     * @param context
     * @return
     * @throws ServiceException
     */
    @Override
    @TerminusParamVilidator(argsIndexs = {0, 1})
    public TerminusResult<List<ContractSellByStagesInfoResult>> gets(ContractSellByStagesInfoGetsParam param, TerminusCallContext context) throws ServiceException {
        ContractSellByStagesInfoGetsDto getsDto = new ContractSellByStagesInfoGetsDto();
        BeanCopyUtils.copyProperties(param, getsDto);
        try {
            List<ContractSellByStagesInfoResultDto> getsResultList = contractSellByStagesInfoManager.gets(getsDto);
            return TerminusResult.success(BeanCopyUtils.copy(getsResultList, ContractSellByStagesInfoResult.class));
        } catch (DataAccessException e) {
            logger.error("查询失败,错误信息：{}", e.getMessage());
            throw new ServiceException(e);
        }
    }

    /**
     * 分页查询
     *
     * @param param
     * @param context
     * @return
     * @throws ServiceException
     */
    @Override
    @TerminusParamVilidator(argsIndexs = {0, 1})
    public TerminusResult<TerminusPageAble<ContractSellByStagesInfoResult>> page(ContractSellByStagesInfoQueryParam param, TerminusPageInfo page, TerminusCallContext context) throws ServiceException {
        ContractSellByStagesInfoQueryDto queryDto = new ContractSellByStagesInfoQueryDto();
        BeanCopyUtils.copyProperties(param, queryDto);
        try {
            TerminusPageAble<ContractSellByStagesInfoResultDto> TerminusPageAble = contractSellByStagesInfoManager.page(queryDto, page);
            if (CollectionUtils.isEmpty(TerminusPageAble.getDatas())) {
                return TerminusResult.success(new TerminusPageAble<>(new ArrayList<>(), page, 0));
            }
            return TerminusResult.success(new TerminusPageAble<>(BeanCopyUtils.copy(TerminusPageAble.getDatas(), ContractSellByStagesInfoResult.class),
                    page, TerminusPageAble.getTotalRows()));
        } catch (DataAccessException e) {
            logger.error("分页查询失败,错误信息：{}", e.getMessage());
            throw new ServiceException(e);
        }

    }
}
